package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.model.TbSpecification;
import com.dongdongshop.model.TbSpecificationExample;
import com.dongdongshop.model.TbSpecificationOption;
import com.dongdongshop.model.TbSpecificationOptionExample;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecificationOptionVO;
import com.dongdongshop.vo.SpecificationVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private TbSpecificationMapper tm;

    @Autowired
    private TbSpecificationOptionMapper tom;

    @Override
    public PageResult<SpecificationVO> selectAll(PageParms<SpecificationVO> parms) {
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        //判断specName是否为空
        if (StringUtils.isNotBlank(parms.getParm().getSpecName())){
            criteria.andSpecNameLike("%" + parms.getParm().getSpecName() + "%");
        }
        PageHelper.startPage(parms.getPageNum(),parms.getPageSize());
        List<TbSpecification> speclist = tm.selectByExample(example);
        PageInfo info = new PageInfo<>(speclist);
        List<SpecificationVO> collect = speclist.stream().map(spec -> {
            SpecificationVO vo = new SpecificationVO();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult<>(collect,info.getTotal(), parms.getPageNum(), parms.getPageSize());
    }

    @Override
    public void addSpec(SpecificationVO specVO, List<SpecificationOptionVO> specOptionList) {
        //增加两张表
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(specVO,tbSpecification);
        tm.addFromId(tbSpecification);//自定义一个sql语句, 获取主键id
        //循环遍历list集合, 并取出内容, 添加到数据库
        for (SpecificationOptionVO option : specOptionList) {
            option.setSpecId(tbSpecification.getId());
            TbSpecificationOption tboption = new TbSpecificationOption();
            BeanUtils.copyProperties(option,tboption);
            tom.insertSelective(tboption);
        }
    }

    @Override
    public SpecificationVO selectById(Long id) {
        TbSpecification tbSpecification = tm.selectByPrimaryKey(id);
        if (tbSpecification==null){
            return null;
        }
        SpecificationVO specVO = new SpecificationVO();
        BeanUtils.copyProperties(tbSpecification,specVO);

        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> optionsList = tom.selectByExample(example);
        List<SpecificationOptionVO> collect = optionsList.stream().map(options -> {
            SpecificationOptionVO optionVO = new SpecificationOptionVO();
            BeanUtils.copyProperties(options, optionVO);
            return optionVO;
        }).collect(Collectors.toList());
        specVO.setSpecOptionList(collect);
        return specVO;
    }

    @Override
    public void updateSpec(SpecificationVO specVO) {
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(specVO,tbSpecification);
        tm.updateByPrimaryKeySelective(tbSpecification);

        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        tbSpecificationOptionExample.createCriteria().andSpecIdEqualTo(specVO.getId());
        tom.deleteByExample(tbSpecificationOptionExample);

        List<SpecificationOptionVO> voList = specVO.getSpecOptionList();
        List<TbSpecificationOption> options = voList.stream().map(vo -> {
            TbSpecificationOption option = new TbSpecificationOption();
            BeanUtils.copyProperties(vo, option);
            option.setSpecId(specVO.getId());
            return option;
        }).collect(Collectors.toList());
        for (TbSpecificationOption option : options) {
            tom.insertSelective(option);
        }
    }

    @Override
    public void delete(Long[] ids) {
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        tbSpecificationExample.createCriteria().andIdIn(Arrays.asList(ids));
        tm.deleteByExample(tbSpecificationExample);

        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        tbSpecificationOptionExample.createCriteria().andSpecIdIn(Arrays.asList(ids));
        tom.deleteByExample(tbSpecificationOptionExample);
    }

    @Override
    public void delThis(Long specId) {
        tom.deleteByPrimaryKey(specId);
    }

    @Override
    public List<SpecificationVO> specList() {
        List<TbSpecification> speclist = tm.selectByExample(null);
        List<SpecificationVO> collect = speclist.stream().map(spec -> {
            SpecificationVO vo = new SpecificationVO();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpecificationOptionVO> specLOptionist() {
        List<TbSpecificationOption> tbSpecificationList = tom.selectByExample(null);
        List<SpecificationOptionVO> voList = tbSpecificationList.stream().map(tbSpecification -> {
            SpecificationOptionVO vo = new SpecificationOptionVO();
            BeanUtils.copyProperties(tbSpecification, vo);
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }
}
